﻿using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.AspNet.Identity.Owin;
using CanDoo.Admin.Core.Models;

namespace CanDoo.Admin.Core.Identity
{
    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // 在此处插入电子邮件服务可发送电子邮件。
            return Task.FromResult(0);
        }
    }

    public class SmsService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // 在此处插入 SMS 服务可发送短信。
            return Task.FromResult(0);
        }
    }

    public class Md5PasswordHasher : IPasswordHasher
    {
        public string HashPassword(string password)
        {
            return CanDoo.Secutiry.PasswordHasher.HashPassword(password);
        }

        public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
        {
            var flag = CanDoo.Secutiry.PasswordHasher.VerifyHashedPassword(hashedPassword, providedPassword);
            var md5 = CanDoo.Secutiry.HashHelper.GetMd5(providedPassword);
            flag |= hashedPassword == md5;
            return flag ? PasswordVerificationResult.Success : PasswordVerificationResult.Failed;
        }
    }


    public class UserManager : UserManager<User,long>,CanDoo.Core.Dependency.IScopeDependency
    {
        public UserManager(IUserStore<User,long> store, IDataProtectionProvider dataProtectionProvider)
            : base(store)
        {
            var manager = this;
            manager.ClaimsIdentityFactory = new CandooClaimsIdentityFactory();
            // 配置用户名的验证逻辑
            manager.UserValidator = new UserValidator<User,long>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = false
            };

            // 配置密码的验证逻辑
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 2,
                RequireNonLetterOrDigit = false,
                RequireDigit = false,
                RequireLowercase = false,
                RequireUppercase = false,
            };

            manager.PasswordHasher = new Md5PasswordHasher();
            // 注册双重身份验证提供程序。此应用程序使用手机和电子邮件作为接收用于验证用户的代码的一个步骤
            // 你可以编写自己的提供程序并将其插入到此处。
            //manager.RegisterTwoFactorProvider("电话代码", new PhoneNumberTokenProvider<User,long>
            //{
            //    MessageFormat = "你的安全代码是 {0}"
            //});
            //manager.RegisterTwoFactorProvider("电子邮件代码", new EmailTokenProvider<User, long>
            //{
            //    Subject = "安全代码",
            //    BodyFormat = "你的安全代码是 {0}"
            //});

            // 配置用户锁定默认值
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 10;

            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();

            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<User,long>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
        }
    }
}
